<?php

/**

 * 短信验证码服务层

 * @author maniac

 */

namespace app\common\service;

use app\common\model\Verify AS VerifyModel;
use think\facade\Log As Log;

class Verify extends Base

{
	
	public function change($mobile)
//	public function change($mobile,$type)

	{
		
		$today = strtotime(date("Y-m-d"),time());
		
		$end = $today+60*60*24;
		
		$where[] = [
		
			['mobile','eq',$mobile],
		
			['create_time','>',$today],
		
			['create_time','<',$end],
		
		];

		$num = VerifyModel::field(true)->where($where)->count();

		if ($num >= 10) {
			
			exception('当前手机号本日获取验证码次数超限');
			
		}
		$Sms = new \Aliyunduanxin\SmsDemo();

		$code = rand('100000','999999');
		
		$mb = "SMS_170842288.....";
		$data['code'] = $code;
// 		Log::write($data,'info');

		$msg = $Sms->sendSms($mobile,$data,$mb,'工殿');

// 		Log::write($msg,'info');
		if ($msg->Code == 'OK') {

			$verify = VerifyModel::create([
			
//					'type'  => $type,
						
					'mobile'   => $mobile,
						
					'code'  => $code,
						
						
					]);
			if ($verify->id) {
					
				return $code;
					
			}
			
			exception('获取验证码失败');
			
		}

		exception('获取验证码失败');
		
	}
	
}

